package com.yy.servlet;

import com.yy.db.StudentDBUtil;
import com.yy.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet(name="UpdateStuBySidServlet",value="/doUpdateStudent")
public class UpdateStuBySidServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println("测试进来没有");
        String sid = req.getParameter("sid");
        StudentDBUtil studentDbUtil = new StudentDBUtil();
        Student student = studentDbUtil.getStuBySid(Integer.valueOf(sid));
        System.out.println(student);
        req.setAttribute("student",student);
        req.getRequestDispatcher("editStudent.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/thml;charset=UTF-8");
        String sid = req.getParameter("sid");
        Student stu=new Student(Integer.valueOf(sid),
                req.getParameter("sname"),
                req.getParameter("sex"),
                req.getParameter("address")
                );


        StudentDBUtil studentDBUtil=new StudentDBUtil();
        studentDBUtil.editStu(stu);
        resp.sendRedirect("/getStudents");
    }
}
